package com.ehotting.mqbd.core.service;

import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.core.util.AgentIdGenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class AgentRefClientService {

    private static Logger logger = LoggerFactory.getLogger(AgentRefClientService.class);

    private TimeUnit TIME_UNIT = TimeUnit.DAYS;
    private Long TIME_OUT = 10L;

    @Autowired
    private RedisService redisService;
    @Autowired
    private AgentRefConsumerService agentRefConsumerService;

    public void setClientIpAndUserIdAndProjectId(String module, Long userId, String projectId, String clientIp){
        String agentId = AgentIdGenUtil.genAgentId(module, userId,projectId);
        logger.info("set agentId:{},clientIp:{} .",agentId,clientIp);
        String key = MQDBConstants.AGENT_REF_CLIENT_IP +  agentId;
        redisService.set(key,clientIp, TIME_OUT,TIME_UNIT);
    }

    public void setClientIpAndAgentId(String agentId, String clientIp){
        logger.info("set agentId:{},clientIp:{} .",agentId,clientIp);
        String key = MQDBConstants.AGENT_REF_CLIENT_IP + agentId;
        redisService.set(key,clientIp, TIME_OUT,TIME_UNIT);
    }

    public void deleteClientIpAndAgentId(String agentId){
        logger.info("Delete agentId:{} .",agentId);
        redisService.delete(MQDBConstants.AGENT_REF_CLIENT_IP +   agentId);
        agentRefConsumerService.deleteConsumerIpByAgentId(agentId);
    }


}
